#include "uart_protocal.h"
#include "ab32_uart.h"
//#include "driver/uart.h"
/*
通信协议制定
以 AA 开头
以 55 结尾

MCU-->8266 [AA addr data1 data2 check 55]

8266-->MCU [55 addr data1 data2 check AA]

addr:地址
00 获取版本号     [muc <-- 8266]
01 灯            [mcu <-- 8266]
02 风扇          [mcu <-- 8266]
03 加热片        [mcu <-- 8266]
04 加湿器        [mcu <-- 8266] 

10 温度         [mcu --> 8266]
11 湿度         [mcu --> 8266]
12 光线         [mcu --> 8266]


data

00 : 获取版本号
data1 56
data2 58

*/
#define ESP_RECV_MAX_LEN (16)
#define ESP_SEND_MAX_LEN (8)
uint8_t esp_rx_buff[ESP_RECV_MAX_LEN];
uint8_t esp_tx_buff[ESP_SEND_MAX_LEN];

static volatile  uint32_t temp_val = 0;
static volatile  uint32_t humi_val = 0;
static volatile  uint32_t light_val = 0;


typedef uint8_t(*CbMsgProc)(uint8_t, uint8_t);

typedef struct
{
    uint8_t cmdCode;
    CbMsgProc msgProc;
} ESP_Send_MsgEntry;

typedef struct
{
    uint8_t cmdCode;
    CbMsgProc msgProc;
} AB_Send_MsgEntry;

void raw_pack_init(uint8_t code, uint8_t data1, uint8_t data2)
{
    esp_tx_buff[0] = 0x55;
    esp_tx_buff[1] = code;
    esp_tx_buff[2] = data1;
    esp_tx_buff[3] = data2;
    esp_tx_buff[4] = (uint8_t)(data1 + data2);
    esp_tx_buff[5] = 0xAA; 
}

//发送版本号
uint8_t esp_send_version(uint8_t data1, uint8_t data2)
{
    raw_pack_init(ESP_VERSION, data1, data2);
    hw_send(&esp_tx_buff[0], 6);
    return 0;
}

//发送LED状态
uint8_t esp_send_led_state(uint8_t data1, uint8_t data2)
{
    raw_pack_init(ESP_LED, data1, data2);
    hw_send(&esp_tx_buff[0], 6);
    return 0;
}

//发送风扇状态
uint8_t esp_send_fan_val(uint8_t data1, uint8_t data2)
{
    raw_pack_init(ESP_FAN, data1, data2);
    hw_send(&esp_tx_buff[0], 6);
    return 0;
}

//发送加热片状态
uint8_t esp_send_ptc_val(uint8_t data1, uint8_t data2)
{
    raw_pack_init(ESP_PTC, data1, data2);
    hw_send(&esp_tx_buff[0], 6);
    return 0;
}

//发送加湿器状态
uint8_t esp_send_humidifier_val(uint8_t data1, uint8_t data2)
{
    raw_pack_init(ESP_HUMIDIFIER, data1, data2);
    hw_send(&esp_tx_buff[0], 6);
    return 0;
}

uint8_t ab_send_temp(uint8_t data1, uint8_t data2)
{
    temp_val = ((data1 * 16) + data2);
    return 0;
}

uint32_t get_temp_status(void)
{
    uint32_t val = 0;

    if(temp_val > 0)
    {
        val = temp_val;
        temp_val = 0;
    }
    return val;
}

uint8_t ab_send_humi(uint8_t data1, uint8_t data2)
{
    humi_val = ((data1 * 16) + data2);
    return 0;
}

uint32_t get_humi_status(void)
{
    uint32_t val = 0;

    if(humi_val > 0)
    {
        val = humi_val;
        humi_val = 0;
    }
    return val;
}

uint8_t ab_send_light(uint8_t data1, uint8_t data2)
{
    light_val = ((data1 * 16) + data2);
    return 0;
}

uint32_t get_light_status(void)
{
    uint32_t val = 0;

    if(light_val > 0)
    {
        val = light_val;
        light_val = 0;
    }
    return val;
}

uint8_t pack_check_sum(uint8_t * buff)
{
    uint8_t ret = 0;
    if((buff[0] == 0xAA) && (buff[5] == 0x55))
    {
        if((uint8_t)(buff[2]+buff[3]) != buff[4])
        {
            ret = 1;
        }
    }
    else
    {
        ret = 1;
    }
    return ret;
}

//esp8266 发送列表
static ESP_Send_MsgEntry msgTable[] =
{
    { ESP_VERSION, esp_send_version },
    { ESP_LED, esp_send_led_state },
    { ESP_FAN, esp_send_fan_val },
    { ESP_PTC, esp_send_ptc_val },
    { ESP_HUMIDIFIER, esp_send_humidifier_val },
};

//AB32 发送过来的数据
static AB_Send_MsgEntry abmsgTable[] =
{
    { AB_TEMP, ab_send_temp },
    { AB_HUMI, ab_send_humi },
    { AB_LIGHT, ab_send_light },
};

//esp8266开始发送消息
void esp_send_msg(uint8_t code, uint8_t data1, uint8_t data2)
{
    int ii = 0;
    CbMsgProc msgProcFunc = 0;
    const int msgNum = sizeof(msgTable) / sizeof(ESP_Send_MsgEntry);
    for (ii = 0; ii < msgNum; ii++)
    {
        if(msgTable[ii].cmdCode == code)
        {
            msgProcFunc = msgTable[ii].msgProc;
            break;
        }
    }

    if(msgProcFunc != 0)
    {
        msgProcFunc(data1, data2);
    }
}

//esp32 接收到串口消息进行处理
void AB32_msgProc(uint8_t *rxBuff, int len)
{
    if((len <= ESP_RECV_MAX_LEN) && (len > 0))
    {
        memcpy(&esp_rx_buff[0], rxBuff, len);

        if(pack_check_sum(&esp_rx_buff[0]) == 0)
        {
            //校验和成功，查找对应的表
            int ii;
            CbMsgProc msgProcFunc = 0;
            const int msgNum = sizeof(abmsgTable) / sizeof(AB_Send_MsgEntry);
            for (ii = 0; ii < msgNum; ii++)
            {
                if (abmsgTable[ii].cmdCode == esp_rx_buff[1])
                {
                    msgProcFunc = abmsgTable[ii].msgProc;
                    break;
                }
            }

            if (msgProcFunc)
            {
                msgProcFunc(esp_rx_buff[2],esp_rx_buff[3]);
            }
        }
    }
}
